package com.best.oasis.settlement.util;


import javax.servlet.http.HttpServletRequest;

import org.springframework.security.Authentication;
import org.springframework.security.context.SecurityContextHolder;

import com.best.oasis.settlement.common.entity.security.User;

import flex.messaging.FlexContext;


public class SecurityUtil {
    /**
     * 获取当前登录的用户名
     * @return
     *          如果用户登录了，就返回登录的用户名；否则返回null
     */
    static public String getLoginUsername(){
        String result = null;
        User user = getLoginUser();
        if(user != null){
            result = user.getUsername();
        }
        return result;
    }
    /**
     * 获取当前登录的用户昵称
     * @return
     *          如果用户登录了，就返回登录的用户昵称；否则返回""
     */
    static public String getLoginUserNick(){
        String result = null;
        User user = getLoginUser();
        if(user != null){
            result = user.getName();
        }
        return result;
    }
    /**
     * 获取当前登录的用户对象
     * @return
     *          如果用户登录了，就返回登录的用户；否则返回null
     */
    static public User getLoginUser(){
        User result = null;
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if(auth == null){
            return result;
        }
        Object o = auth.getPrincipal();
        if(o != null && o instanceof User){
            result = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        }else{
            result = null;
        }
        return result;
    }
    /**
     * 获取访问用户的ip
     * @return
     */
    public static String getUserIp() {
        String result;
        HttpServletRequest request = null;
        try{
            request = FlexContext.getHttpRequest();
        }catch(Exception e){
            
        }
        if(request == null){
            return "unknown host";
        }
        result = request.getRemoteAddr();
        
        return result;
    }
    /**
     * 获取访问url
     * @return
     */
    public static String getRequestUrl() {
        String result;
        HttpServletRequest request = null;
        try{
            request = FlexContext.getHttpRequest();
        }catch(Exception e){
            
        }
        if(request == null){
            return "unknown host";
        }               
        String uri = request.getRequestURI();      
        String url =  request.getRequestURL().toString();  
        result=url.substring(0, url.indexOf(uri))+request.getContextPath();
        
        return result;
    }
    /**
     * 获取访问ContextPath
     * @return
     */
    public static String getContextPath() {
        String result;
        HttpServletRequest request = null;
        try{
            request = FlexContext.getHttpRequest();
        }catch(Exception e){
            
        }
        if(request == null){
            return "unknown host";
        }        
        result =  request.getSession().getServletContext().getRealPath("/");        
        return result;
    }
}
